package org.gitos.cloud.util;

/**
 * Created by shizhengchu on 16/1/13.
 */
public class ByteUtil {

    public static final int BIG_ENDIAN = 0;
    public static final int LITTLE_ENDIAN = 1;

    public static byte[] int2Bytes(int value) {
        return int2Bytes(value, BIG_ENDIAN);
    }

    public static byte[] int2Bytes(int value, int order) {
        byte[] v = new byte[]{0x00, 0x00, 0x00, 0x00};
        if (order == BIG_ENDIAN) {
            v[3] = (byte) value;
            v[2] = (byte) (value >>> 8);
            v[1] = (byte) (value >>> 16);
            v[0] = (byte) (value >>> 24);
        } else {
            v[0] = (byte) value;
            v[1] = (byte) (value >>> 8);
            v[2] = (byte) (value >>> 16);
            v[3] = (byte) (value >>> 24);
        }
        return v;
    }

    public static byte[] short2Bytes(short value) {
        return short2Bytes(value, BIG_ENDIAN);
    }

    public static byte[] short2Bytes(short value, int order) {
        byte[] v = new byte[]{0x00, 0x00};
        if (order == BIG_ENDIAN) {
            v[1] = (byte) value;
            v[0] = (byte) (value >>> 8);
        } else {
            v[0] = (byte) value;
            v[1] = (byte) (value >>> 8);
        }
        return v;
    }


}
